package com.course.server.service;/**
 * @author: lin
 */

import com.course.server.domain.CourseContentFile;
import com.course.server.domain.CourseContentFileExample;
import com.course.server.dto.CourseContentFileDto;
import com.course.server.mapper.CourseContentFileMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
/**
 *@Author lin
 *@Data Created in 2020/7/28 21:21
 *Description 
 *@Modified By
 *@Version V1.0
 */
@Service
public class CourseContentFileService {
    @Resource
    private CourseContentFileMapper courseContentFileMapper;
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:40
     */
    public  List<CourseContentFileDto> list(String courseId){
        CourseContentFileExample example = new CourseContentFileExample();
        CourseContentFileExample.Criteria criteria = example.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        List<CourseContentFile> fileList = courseContentFileMapper.selectByExample(example);
        return CopyUtil.copyList(fileList,CourseContentFileDto.class);
    }
    /**
     * create by: lin
     * description: 保存
     * create time: 2020/8/2 17:40
     */
    public void save(CourseContentFileDto courseContentFileDto){
        CourseContentFile courseContentFile = CopyUtil.copy(courseContentFileDto, CourseContentFile.class);
        if(StringUtils.isEmpty(courseContentFileDto.getId())){
            //StringUtils.isEmpty spring的工具类来判断是否有值
            this.insert(courseContentFile);
        }else {
            this.update(courseContentFile);
        }
    }
    /**
     * create by: lin
     * description: 新增
     * create time: 2020/8/2 17:41
     */
    private void insert(CourseContentFile courseContentFile){
        courseContentFile.setId(UuidUtil.getShortUuid());
        courseContentFileMapper.insert(courseContentFile);
    }
    /**
     * create by: lin
     * description: 修改
     * create time: 2020/8/2 17:58
     */
    private void update(CourseContentFile courseContentFile){
        courseContentFileMapper.updateByPrimaryKey(courseContentFile);
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:41
     */
    public void delete(String id){
        courseContentFileMapper.deleteByPrimaryKey(id);
    }
}
